package com.runa.rccprotocol.dto.qiyang;

import com.runa.monitor.comm.dto.newComm.HotMeterVO;
import com.runa.persistence.DateUtil;
import com.runa.persistence.service.LogService;
import com.runa.protocol.analysis.MetaBufferAnalysis;
import com.runa.protocol.util.ByteUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * 2024/7/12 10:24
 * zhangYB
 */
@Slf4j
public class QyConcentratorResponseAnalysis extends MetaBufferAnalysis<byte[]> {


    @Override
    public int fill(byte[] data, int length, String netEquNo) {
        try {
            QyConcentratorResponseData response =  new QyConcentratorResponseData();
            response.setAddress(netEquNo);
            response.setMessage(ByteUtil.byteToHexString(data, 0, length));
            this.responseData = response;

            HotMeterVO qyEqu = new HotMeterVO();
            qyEqu.setReadDate(new Date());
            int index = 0;
            String serial = ByteUtil.bcd2str(data, index, 4, false);
            LogService.saveErrorLog("七洋:"+netEquNo+":"+serial, "QY", data, length);
            qyEqu.setMeterSerial(serial);

            boolean unread = true;
            for (int i = 4; i < length - 1; i++) {
                if (!((data[i] & 0xff) == 0)) {
                    unread = false;
                    break;
                }
            }
            if (unread) {
                qyEqu.setSuccess(false);
                response.addData(qyEqu);
                return 0;
            }



            index += 4;
            float accCold = (float) (Float.valueOf(ByteUtil.bcd2str(data, index, 4, false)) * 0.01);
//            qyEqu.setAccCold(String.valueOf(accCold));

            index += 5;
            float accHeat = (float) (Float.valueOf(ByteUtil.bcd2str(data, index, 4, false)) * 0.01);
            qyEqu.setAccHeat(accHeat);

            index += 5;
            float power = (float) (Float.valueOf(ByteUtil.bcd2str(data, index, 4, false)) * 0.01);
            qyEqu.setPower(power);

            index += 5;
            float flow = (float) (Float.valueOf(ByteUtil.bcd2str(data, index, 4, false)) * 0.0001);
            qyEqu.setFlow(flow);

            index += 5;
            float accFlow = (float) (Float.valueOf(ByteUtil.bcd2str(data, index, 4, false)) * 0.01);
            qyEqu.setAccFlow(accFlow);

            index += 5;
            float supplyTemp = (float) (Float.valueOf(ByteUtil.bcd2str(data, index, 3, false)) * 0.01);
            qyEqu.setSupplyTemp(supplyTemp);

            index += 3;
            float returnTemp = (float) (Float.valueOf(ByteUtil.bcd2str(data, index, 3, false)) * 0.01);
            qyEqu.setReturnTemp(returnTemp);

            index += 3;
            String workTime = ByteUtil.bcd2str(data, index, 3, false);
            qyEqu.setWorkTime(Integer.valueOf(workTime));

            index += 3;
            String equTime = DateUtil.toDateString(ByteUtil.byteToHexStringFlip(data, index, 7));
            qyEqu.setMeterDate(DateUtil.toDateGeneric(equTime));

            index += 7;
            String status = ByteUtil.bcd2str(data, index, 2, false);
            qyEqu.setStatus(status);

            response.addData(qyEqu);

        } catch (Exception e) {
            log.error("七洋： {} {} {}", netEquNo, ByteUtil.byteToHexString(data, 0, length), e);
        }
        return 0;
    }





}
